#include "stdafx.h"
#include "net/PartStore.hpp"
#include "filesys/file.hpp"
#include "filesys/temporaryfile.hpp"
using namespace JHCPP::filesys;


NET_NAMESPACE_BEGIN

//
// CPartStore
//
CPartStore::CPartStore(const std::string& mediaType) : CPartSource(mediaType)
{
}

CPartStore::~CPartStore()
{
}


//
// CFilePartStore
//
CFilePartStore::CFilePartStore(const std::string& content, const std::string& mediaType, const std::string& filename)
	: CPartStore(mediaType),
	m_filename(filename),
	m_path(CTemporaryFile::tempName()),
	m_fstr(m_path)
{
	m_fstr << content << std::flush;
	m_fstr.seekg(0, std::ios::beg);
}

CFilePartStore::~CFilePartStore()
{
	try
	{
		m_fstr.close();
		CFile(m_path).remove();
	}
	catch (...)
	{
	}
}

std::istream& CFilePartStore::stream()
{
	return m_fstr;
}

const std::string& CFilePartStore::filename() const
{
	return m_filename;
}

const std::string& CFilePartStore::path() const
{
	return m_path;
}

NET_NAMESPACE_END
